<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>树数据的搜索</title>
    <link href="zTreeStyle/zTreeStyle.css" rel="stylesheet" />
    <!-- jquery包，ztree依赖jquery --> 
    <script type="text/javascript" src="jquery-1.4.4.min.js"></script>

    <!-- ztree核心包，ztree核心功能 -->    
    <script type="text/javascript" src="jquery.ztree.core.min.js"></script>

    <!-- ztree exhide包，ztree隐藏显示结点需要此包支持 -->   
    <script type="text/javascript" src="jquery.ztree.exhide.min.js"></script>
</head>

<body>
    <div class="container">
        <div class="search-bar">
            <input id="keyword" type="text" placeholder="请输入...">
            <button id="search">搜索</button>
        </div>
        <div class="content">
            <!-- 用于显示ztree的html元素的class一定要设置为ztree-->
            <ul id="ztreeObj" class="ztree"></ul>
        </div>
    </div>
<script>
let data = {
    "code": "0",
    "isSuccess": true,
    "message": "ok!",
    "errorCode": "0",
    "data": {
        "treeType": "0",
        "treeData": {
            "nodeId": "3884",
            "nodeName": "高力物业",
            "parentId": "0",
            "nodeType": "1",
            "parent": true,
            "layer": "0",
            "authFlag": false,
            "childList": [
                {
                    "nodeId": "3886",
                    "nodeName": "常州分公司",
                    "parentId": "3884",
                    "nodeType": "3",
                    "parent": true,
                    "layer": "1",
                    "authFlag": false,
                    "childList": [
                        {
                            "nodeId": "3962",
                            "nodeName": "常州字母项目部",
                            "parentId": "3886",
                            "nodeType": "4",
                            "parent": true,
                            "layer": "2",
                            "authFlag": false,
                            "childList": [
                                {
                                    "nodeId": "445357915730083840",
                                    "nodeName": "常州A项目",
                                    "parentId": "3962",
                                    "nodeType": "5",
                                    "layer": null,
                                    "authFlag": false,
                                    "parent": false
                                }
                            ]
                        },
                        {
                            "nodeId": "3902",
                            "nodeName": "常州汽配城服务中心",
                            "parentId": "3886",
                            "nodeType": "4",
                            "parent": true,
                            "layer": "2",
                            "authFlag": false,
                            "childList": [
                                {
                                    "nodeId": "422348265879506944",
                                    "nodeName": "常州汽配城",
                                    "parentId": "3902",
                                    "nodeType": "5",
                                    "layer": null,
                                    "authFlag": false,
                                    "parent": false
                                }
                            ]
                        }
                    ]
                },
                {
                    "nodeId": "3887",
                    "nodeName": "扬州分公司",
                    "parentId": "3884",
                    "nodeType": "3",
                    "parent": true,
                    "layer": "1",
                    "authFlag": false,
                    "childList": [
                        {
                            "nodeId": "3910",
                            "nodeName": "扬州汽配城服务中心",
                            "parentId": "3887",
                            "nodeType": "4",
                            "parent": true,
                            "layer": "2",
                            "authFlag": false,
                            "childList": [
                                {
                                    "nodeId": "426389306123227136",
                                    "nodeName": "扬州汽配城",
                                    "parentId": "3910",
                                    "nodeType": "5",
                                    "layer": null,
                                    "authFlag": false,
                                    "parent": false
                                }
                            ]
                        },
                        {
                            "nodeId": "3913",
                            "nodeName": "扬州家居港服务中心",
                            "parentId": "3887",
                            "nodeType": "4",
                            "parent": true,
                            "layer": "2",
                            "authFlag": false,
                            "childList": [
                                {
                                    "nodeId": "423849444853682176",
                                    "nodeName": "扬州家居港",
                                    "parentId": "3913",
                                    "nodeType": "5",
                                    "layer": null,
                                    "authFlag": false,
                                    "parent": false
                                }
                            ]
                        }
                    ]
                },
                {
                    "nodeId": "3888",
                    "nodeName": "无锡分公司",
                    "parentId": "3884",
                    "nodeType": "3",
                    "parent": true,
                    "layer": "1",
                    "authFlag": false,
                    "childList": [
                        {
                            "nodeId": "3895",
                            "nodeName": "无锡汽配城服务中心",
                            "parentId": "3888",
                            "nodeType": "4",
                            "parent": true,
                            "layer": "2",
                            "authFlag": false,
                            "childList": [
                                {
                                    "nodeId": "423857741933580288",
                                    "nodeName": "无锡汽配城",
                                    "parentId": "3895",
                                    "nodeType": "5",
                                    "layer": null,
                                    "authFlag": false,
                                    "parent": false
                                }
                            ]
                        }
                    ]
                },
                {
                    "nodeId": "3889",
                    "nodeName": "盐城分公司",
                    "parentId": "3884",
                    "nodeType": "3",
                    "parent": true,
                    "layer": "1",
                    "authFlag": false,
                    "childList": [
                        {
                            "nodeId": "3909",
                            "nodeName": "盐城金色果缘服务中心",
                            "parentId": "3889",
                            "nodeType": "4",
                            "parent": true,
                            "layer": "2",
                            "authFlag": false,
                            "childList": [
                                {
                                    "nodeId": "426354255696236544",
                                    "nodeName": "金色果缘",
                                    "parentId": "3909",
                                    "nodeType": "5",
                                    "layer": null,
                                    "authFlag": false,
                                    "parent": false
                                }
                            ]
                        },
                        {
                            "nodeId": "3912",
                            "nodeName": "盐城家居港服务中心",
                            "parentId": "3889",
                            "nodeType": "4",
                            "parent": true,
                            "layer": "2",
                            "authFlag": false,
                            "childList": [
                                {
                                    "nodeId": "432567135370215424",
                                    "nodeName": "盐城家居港",
                                    "parentId": "3912",
                                    "nodeType": "5",
                                    "layer": null,
                                    "authFlag": false,
                                    "parent": false
                                }
                            ]
                        }
                    ]
                },
                {
                    "nodeId": "3890",
                    "nodeName": "长春分公司",
                    "parentId": "3884",
                    "nodeType": "3",
                    "parent": true,
                    "layer": "1",
                    "authFlag": false,
                    "childList": [
                        {
                            "nodeId": "3911",
                            "nodeName": "长春汽贸城服务中心",
                            "parentId": "3890",
                            "nodeType": "4",
                            "parent": true,
                            "layer": "2",
                            "authFlag": false,
                            "childList": [
                                {
                                    "nodeId": "425955278068912128",
                                    "nodeName": "长春汽配城",
                                    "parentId": "3911",
                                    "nodeType": "5",
                                    "layer": null,
                                    "authFlag": false,
                                    "parent": false
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        "description": "描述：0全量树,1节点树"
    }
};

let treeData = [data.data.treeData];
console.log(treeData)

//ztree配置
var setting = {
    view: {
        // 使用ztree自定义高亮时，一定要设置fontCss,setHighlight是自定义高亮方法
        //fontCss: setHighlight,
        nameIsHTML: true, //允许name支持html                
        selectedMulti: false
    },
    data: {
        key: {
            // nodeType: 'customType',
            name: 'nodeName',
            title: 'nodeName',
            children: 'childList'
        }
    }
};

function initThisZtree(data){
    //初始化ztree三个参数分别是(jQuery对象,ztree设置,树节点数据)
    var treeObj = $.fn.zTree.init($("#ztreeObj"), setting, data);
}

initThisZtree(treeData);

let showParentNodes = []; // 要显示的父级节点
let hiddenParentNodes = []; // 要隐藏的父级节点
let hiddenNodes =[];
/**
 * 查找当前结点和父结点，如果找到，返回ture，否则返回false
 */
function searchShowParent(node){
    let parentNode = node.getParentNode();
    if (parentNode) {
        let find = showParentNodes.find(x => x.nodeId === parentNode.nodeId);
        if (!find) { showParentNodes.push(parentNode); }
        searchShowParent(parentNode);
    }
}
function searchHiddenParent(node){
    let parentNode = node.getParentNode();
    if (parentNode) {
        let find = hiddenParentNodes.find(x => x.nodeId === parentNode.nodeId);
        if (!find) { hiddenParentNodes.push(parentNode); }
        searchHiddenParent(parentNode);
    }
}

$('#search').click(function(){
    var ztreeObj = $.fn.zTree.getZTreeObj("ztreeObj");    
    ztreeObj.showNodes(hiddenNodes.concat(hiddenParentNodes));
    showParentNodes = [];
    hiddenParentNodes = [];

    function filter(node) {
        var keyword=$("#keyword").val();
        if (!node.parent) { // 如果是叶子节点
            if (node.nodeName.indexOf(keyword) !== -1) {
                searchShowParent(node);
            } else {
                searchHiddenParent(node);
            }
        }
        if(node.parent||node.nodeName.indexOf(keyword)!=-1) return false;
        return true;
    }
    hiddenNodes = ztreeObj.getNodesByFilter(filter); // 查找节点集合
    console.log(hiddenNodes);
    console.log(showParentNodes, hiddenParentNodes);
    let realHiddenParents = hiddenParentNodes.filter(p => {
        let find = showParentNodes.find(x => x.nodeId === p.nodeId);
        return find ? false : true;
    })
    console.log('需隐藏的父节点', realHiddenParents);
    //隐藏不符合条件的叶子结点
    ztreeObj.hideNodes(hiddenNodes.concat(realHiddenParents));
});
</script>
</body>

</html>